﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.Globalization;
using Medianamik.UI.TypeManagement.Controls;

namespace Medianamik.UI.TypeManagement
{
    //TODO: Valider le changement de MedianamikControl 
    // --> certaines propriétés (ex. Unique) dépendent du MedianamikControl
    //Vincent propose un système de log pour les actions --> à voir
    //Pour l'instant, empêcher de changer le DataType et le MedianamikControl si le type a des noeuds

    public partial class InterfaceManagement : BasePage
    {
        #region Fields

        private Guid? _interfaceId;
        protected const string ReturnUrl = "interfacelist.aspx";

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            #region Param - Interface Id

            var interfaceStringId = Request.QueryString["interfaceid"];

            if (!String.IsNullOrEmpty(interfaceStringId)) //Édition d'une interface
            {
                if (!StringHelper.IsGuid(interfaceStringId, ref _interfaceId) || CurrentInterface == null)
                {
                    Response.Redirect(ReturnUrl);
                }
            }

            #endregion

            if (IsPostBack) return;

            if (IsEdition)
            {
                SetValues();
            }
        }

        protected void ValidateUniqueInterfaceName(object source, ServerValidateEventArgs args)
        {
            args.IsValid = !((IsEdition && CurrentInterface.Name != InterfaceName.Text &&
                          NodeTypeManager.NodeTypeInterfaceExists(InterfaceName.Text))
                          || (!IsEdition && NodeTypeManager.NodeTypeInterfaceExists(InterfaceName.Text)));
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                if (IsEdition)
                {
                    CurrentInterface.Name = InterfaceName.Text;
                    CurrentInterface.Description = InterfaceDescription.Text;
                }
                else
                {
                    _currentInterface = new NodeTypeInterface(InterfaceName.Text,
                        InterfaceDescription.Text);
                }

                NodeTypeManager.SaveInterface(CurrentInterface);

                Translator.CreateResource("TypeManagement", CurrentInterface.ID + "_Label", LabelTranslatorBox.Values);

                Response.Redirect(ReturnUrl);
            }
        }

        #endregion

        #region Properties

        private NodeTypeInterface _currentInterface;
        public NodeTypeInterface CurrentInterface
        {
            get
            {
                if (_currentInterface == null && _interfaceId.HasValue)
                {
                    _currentInterface = NodeTypeManager.GetNodeTypeInterface(_interfaceId.Value);
                }

                return _currentInterface;
            }
        }

        private bool IsEdition
        {
            get
            {
                return _interfaceId.HasValue;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Affecte les valeurs aux champs de la page
        /// </summary>
        private void SetValues()
        {
            #region Name and description

            var implementers = NodeTypeManager.GetNodeTypes(CurrentInterface.ID);

            InterfaceName.Text = CurrentInterface.Name;
            InterfaceDescription.Text = CurrentInterface.Description;
            InterfaceName.Enabled = implementers.Count() < 1;

            #endregion

            #region Translations

            LabelTranslatorBox.Values = Translator.GetResources("TypeManagement",
                CurrentInterface.ID + "_Label",
                ConfigManager.GetMedianamikCultures().Select(c => c.CultureInfo));

            #endregion
        }

        #endregion
    }
}
